//
//  TTPriceChangeView.m
//  TaoTuo
//
//  Created by LG on 2020/9/13.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTPriceChangeView.h"
#import <UIView+TYAlertView.h>

@interface TTPriceChangeView ()

@property (weak, nonatomic) IBOutlet UIButton *subBtn;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@property (weak, nonatomic) IBOutlet UITextField *priceTF;
@property (weak, nonatomic) IBOutlet UILabel *chujiaInfoLabel;
@property (weak, nonatomic) IBOutlet UILabel *rateLabel;
/// 当前价格
@property (nonatomic, assign) int currentPrice;
/// 加价幅度
@property (nonatomic, assign) int priceRate;

@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, assign) int count;
/// 最小出价值
@property (nonatomic, assign) int minPrice;

@end

@implementation TTPriceChangeView

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.layer.cornerRadius = 10;
    self.layer.masksToBounds = YES;
    
    self.subBtn.layer.cornerRadius = 15;
    self.addBtn.layer.cornerRadius = 15;
    
//    self.priceTF.layer.borderColor = UIColorHex(#DDDDDD).CGColor;
//    self.priceTF.layer.borderWidth = 1;
    
    @weakify(self)
    [self.priceTF.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        if ([x intValue] <= self.minPrice) {
            self.subBtn.enabled = NO;
        } else {
            self.subBtn.enabled = YES;
        }
    }];
    
    [RACObserve(self.subBtn, enabled) subscribeNext:^(id  _Nullable x) {
        UIColor *color = [x boolValue] ? UIColorHex(#DF4343) : UIColorHex(#CCCCCC);
        self.subBtn.backgroundColor = color;
    }];
    
    
}

- (void)setupWithCurrentPrice:(int)currentPrice startPrice:(int)startPrice priceRate:(int)priceRate
{
    self.priceRate = priceRate;
    
    self.currentPrice = currentPrice;
    self.count = currentPrice + priceRate;
    self.priceTF.text = [NSString stringWithFormat:@"%d", self.count];
    
    self.minPrice = self.currentPrice + self.priceRate;
     
    UIColor *color = UIColorHex(#DF4343);
    CGFloat fontSize = 14;
    NSString *price = [NSString stringWithFormat:@"¥%d", currentPrice];
    if (currentPrice == 0) {
        price = @"未出价";
        color = UIColorHex(666666);
        fontSize = 13;
        self.currentPrice = startPrice;
        self.count = startPrice;
        self.priceTF.text = [NSString stringWithFormat:@"%d", startPrice];
        self.minPrice = self.currentPrice;
    }
    NSString *str = [NSString stringWithFormat:@"您的当前出价：%@", price];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
    [attr addAttributes:@{NSForegroundColorAttributeName : UIColorHex(#DF4343), NSFontAttributeName : [UIFont systemFontOfSize:9]} range:[str rangeOfString:@"¥"]];
    [attr addAttributes:@{NSForegroundColorAttributeName : color, NSFontAttributeName : [UIFont systemFontOfSize:fontSize]} range:[str rangeOfString:price]];
    self.chujiaInfoLabel.attributedText = attr;
    
    NSString *rateStr = [NSString stringWithFormat:@"%d", priceRate];
    NSString *rateInfoStr = [NSString stringWithFormat:@"加价幅度：¥%@", rateStr];
    NSMutableAttributedString *attr1 = [[NSMutableAttributedString alloc] initWithString:rateInfoStr];
    [attr1 addAttributes:@{NSForegroundColorAttributeName : UIColorHex(#DF4343), NSFontAttributeName : [UIFont systemFontOfSize:9]} range:[rateInfoStr rangeOfString:@"¥"]];
    [attr1 addAttributes:@{NSForegroundColorAttributeName : UIColorHex(#DF4343), NSFontAttributeName : [UIFont systemFontOfSize:14]} range:[rateInfoStr rangeOfString:rateStr]];
    self.rateLabel.attributedText = attr1;
    
}

///减号按下在里面抬起
- (IBAction)subBtnTouchUpInside:(id)sender {
    
    [self subAction];
    
    [self.timer invalidate];
    self.timer = nil;
    
}

///减号按下在外面抬起
- (IBAction)SubBtnTouchUpOutside:(id)sender {
    
    [self.timer invalidate];
    self.timer = nil;
}

// 减号按下
- (IBAction)SubBtnTouchDown:(id)sender {
    
    YYWeakProxy *proxy = [YYWeakProxy proxyWithTarget:self];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:proxy selector:@selector(subAction) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}

- (void)subAction
{
    if (self.count <= self.minPrice) {
        self.subBtn.enabled = NO;
        [self.timer invalidate];
        self.timer = nil;
        return;
    }
    
    self.count -= self.priceRate;
    self.priceTF.text = [NSString stringWithFormat:@"%d", self.count];
}




- (IBAction)addBtnTouchUpInside:(id)sender {
    
    [self addAction];
    [self.timer invalidate];
    self.timer = nil;
    
}

- (IBAction)addBtnTouchUpOutside:(id)sender {
    
    [self.timer invalidate];
    self.timer = nil;
}

- (IBAction)addBtnTouchDown:(id)sender {
    
    YYWeakProxy *proxy = [YYWeakProxy proxyWithTarget:self];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:proxy selector:@selector(addAction) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}

- (void)addAction
{
    self.count += self.priceRate;
    self.priceTF.text = [NSString stringWithFormat:@"%d", self.count];

    if (self.count > self.minPrice) {
        self.subBtn.enabled = YES;
    }
    
}


// 出价
- (IBAction)bottomBtnClick:(UIButton *)sender {
    
    if (self.priceTF.text.intValue < self.minPrice) {
        // 价格偏低，不行、
        [[UIApplication sharedApplication].keyWindow makeToast:@"出价金额过低！"];
    } else {
        // 可以出价
        [self dismissWithAnimationDuration:0.3];
        @weakify(self)
        LLSystemAlertView *alertView = [LLSystemAlertView alertViewWithTitle:@"" message:@"您确定出价吗？" cancelTitle:@"取消" cancelBlock:^{} confirmTitle:@"确定" confirmBlock:^BOOL{
            @strongify(self)
            if (self.confirmPrice) {
                self.confirmPrice(self.priceTF.text);
            }
            
            return YES;
        }];
        alertView.cancleTitleColor = UIColorHex(666666);
        
        [TYShowAlertView showAlertViewWithView:alertView backgoundTapDismissEnable:YES];
        
        
    }
}


- (IBAction)closeBtnClick:(UIButton *)sender {
    
    [self hideView];
}


@end
